package category.action;

import blogData.entity.BlogDataVO;
import blogData.service.BlogDataService;
import category.entity.CategoryVO;
import category.service.CategoryService;
import com.opensymphony.xwork2.ActionSupport;
import generic.page.Pages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.ObtainOther;

public class CategoryAction extends ActionSupport {

    private final static Log LOGER = LogFactory.getLog(CategoryAction.class);

    private CategoryService categoryService;

    private CategoryVO categoryVO;

    private Pages pages;

    private String message;

    private BlogDataService blogDataService;

    private Integer blogDataId;

    private Integer categoryId;

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public Integer getBlogDataId() {
        return blogDataId;
    }

    public void setBlogDataId(Integer blogDataId) {
        this.blogDataId = blogDataId;
    }

    public BlogDataService getBlogDataService() {
        return blogDataService;
    }

    public void setBlogDataService(BlogDataService blogDataService) {
        this.blogDataService = blogDataService;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public Pages getPages() {
        return pages;
    }

    public void setPages(Pages pages) {
        this.pages = pages;
    }

    public CategoryVO getCategoryVO() {
        return categoryVO;
    }

    public void setCategoryVO(CategoryVO categoryVO) {
        this.categoryVO = categoryVO;
    }

    /**
     * 添加分类
     *
     * @return
     */
    public String addCategory() {
        try {
            blogDataId = ObtainOther.obtainId();
            BlogDataVO blogDataVO = blogDataService.findByBlogDataId(Integer.valueOf(blogDataId));
            categoryVO = categoryService.addCategoryVO(blogDataVO, categoryVO);
            message= ObtainOther.obtainMessage("addCategorySuccess");
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("addCategoryError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 删除分类
     *
     * @return
     */
    public String deleteCategory() {
        try {
            categoryService.deleteCategoryById(categoryId);
            message= ObtainOther.obtainMessage("deleteCategorySuccess");
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("deleteCategoryError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 更新分类
     *
     * @return
     */
    public String updateCategory() {
        try {
            categoryVO.setArticleNumber(categoryService.countArticleByCategory(categoryVO.getCategoryId()));
            categoryVO = categoryService.updateCategory(categoryVO);
            message= ObtainOther.obtainMessage("updateCategorySuccess");
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("updateCategoryError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 分页显示分类
     * @return
     */
    public String listCategory() {
        try {
            blogDataId = ObtainOther.obtainId();
            pages = categoryService.queryForPage(pages,blogDataId);
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("listCategoryError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }

    /**
     * 读取分类
     * @return
     */
    public String loadCategory() {
        try {
            categoryVO = categoryService.findCategoryVOById(categoryId);
            categoryVO.setArticleNumber(categoryService.countArticleByCategory(categoryVO.getCategoryId()));
            return SUCCESS;
        } catch (Exception e) {
            message= ObtainOther.obtainMessage("loadCategoryError");
            LOGER.error(message + ":/n" + e);
            return ERROR;
        }
    }
}
